﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

namespace IService.DataDLL
{
    /// <summary>
    /// 更新数据库的方法类
    /// </summary>
    internal class UpdateMethod : BaseMethod
    {
        private static Hashtable UpdateClauseStore = new Hashtable();
        public UpdateMethod(Type infoType)
            : base(infoType)
        {
        }
        public UpdateMethod(BaseInfo info)
            : base(info)
        {
        }
        public UpdateMethod(string TableName, BaseInfo info)
            : base(TableName, info)
        {
        }

        public bool Update()
        {
            string CommandText = UpdateClause;
            bool result = base.ExecuteBool(CommandText, base.GetParameters(false));
            return result;
        }

        /// <summary>
        /// 通过条件更新所有字段
        /// </summary>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public bool Update(string WhereClause)
        {
            if (base.Entity == null)
            {
                return false;
            }
            return this.Update(WhereClause, base.GetParameters(false));
        }

        /// <summary>
        /// 通过条件更新数据库
        /// </summary>
        /// <param name="WhereClause"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public bool Update(string WhereClause, IDataParameter[] parameters)
        {
            if (base.InfoType == null)
            {
                return false;
            }
            try
            {
                StringBuilder sb_sql = new StringBuilder();
                sb_sql.Append(this.UpdateClause);
                if (!string.IsNullOrEmpty(WhereClause))
                {
                    sb_sql.AppendFormat(" WHERE {0}", WhereClause);
                }
                sb_sql.Append(';');
                return base.ExecuteNonQuery(sb_sql.ToString(), parameters) > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 通过三参数来更新数据库，set {0} where {1}
        /// </summary>
        /// <param name="SetClause"></param>
        /// <param name="WhereClause"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public bool Update(string SetClause, string WhereClause, IDataParameter[] parameters)
        {
            if (base.InfoType == null)
            {
                return false;
            }
            try
            {
                StringBuilder sb_update = new StringBuilder();
                sb_update.AppendFormat("UPDATE [{0}] SET {1}", base.TableName, SetClause);
                if (!string.IsNullOrEmpty(WhereClause))
                {
                    sb_update.AppendFormat(" WHERE {0}", WhereClause);
                }
                return base.ExecuteNonQuery(sb_update.ToString(), parameters) > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 通过自增列更新数据库
        /// </summary>
        /// <returns></returns>
        public bool UpdateByIdentity()
        {
            if (!((base.Entity != null) && base.HasIdentity))
            {
                return false;
            }
            return this.Update(base.IdentityWhereClause);
        }

        protected string UpdateCommandText(StringBuilder sb_update)
        {
            sb_update.AppendFormat("UPDATE [{0}] SET ", base.TableName);
            base.AppendParameterString(sb_update, "[{0}]=@{0}", ", ", false);
            return sb_update.ToString();
        }

        /// <summary>
        /// 更新SQL
        /// </summary>
        public string UpdateClause
        {
            get
            {
                string result = UpdateClauseStore[base.TableName] as string;
                if (result == null)
                {
                    StringBuilder sb_update = new StringBuilder();
                    this.UpdateCommandText(sb_update);
                    UpdateClauseStore[base.TableName] = result = sb_update.ToString();
                }
                return result;
            }
        }
    }
}
